package com.simons.common.utils.code;

import org.apache.commons.lang3.StringUtils;

/**
 * 未修订版----------

 * @Description:TODO

 * @author:未知

 * @time:2017年10月31日 上午9:57:25
 */
public class RandomGenerator {
	public RandomGenerator() {
	}

	public static String randomPassword() {
		return randomString(6);
	}

	public static String randomString(int length) {
		return randomString(length, true);
	}

	public static String randomString(int length, boolean includeNumbers) {
		StringBuffer b = new StringBuffer(length);
		for (int i = 0; i < length; i++)
			if (includeNumbers)
				b.append(randomCharacter());
			else
				b.append(randomAlpha());
		return b.toString();
	}

	public static char randomCharacter() {
		int i = (int) (Math.random() * 3D);
		if (i < 2)
			return randomAlpha();
		else
			return randomDigit();
	}

	public static char randomAlpha() {
		int i = (int) (Math.random() * 52D);
		if (i > 25)
			return (char) ((97 + i) - 26);
		else
			return (char) (65 + i);
	}

	public static char randomDigit() {
		int i = (int) (Math.random() * 10D);
		return (char) (48 + i);
	}

	/**
	 * 生成随机字符串
	 * 
	 * @param length
	 * @return
	 */
	public static String randomStringNum(int length) {
		long size = 1;
		for (int i = 1; i <= length; i++) {
			size *= 10;
		}
		return StringUtils.rightPad(Math.round(Math.random() * size) + "", length, "0");
	}
}
